package com.rongc.client.core.pay;

import android.app.Activity;
import android.text.TextUtils;

import com.rongc.client.core.pay.alipay.AlipayHelper;
import com.rongc.client.core.pay.alipay.PayResult;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.utils.UtilMethod;


public class DoAlipay {

	public interface OnPayCompleteResyltListener{
		void OnResult(String resultStr, String txt, boolean result);
	}

	/**
	 * 获取支付宝帐号信息
	 * @param act
	 * @param listener
	 */
	public void doAlipay(final String Orid, final String uid,
						 final Activity act, final OnPayCompleteResyltListener listener){
		UtilMethod.showProgressDialog(act, "获取支付宝信息...");
		/*MatchRequest.doPay(Orid, uid,  new ResponseHandler() {

			@Override
			public void handleResponse(Object obj) {
				JSONObject result = (JSONObject) obj;
				UtilMethod.dismissProgressDialog(act);
				String alipayjson = result.optString("data");

				payAlipay(alipayjson, act, listener);
			}

			@Override
			public void handleException(Object obj) {
				UtilMethod.dismissProgressDialog(act);
				ActivityUtils.toast("获取支付宝信息失败");
			}
		}, act, new HttpSetting(false));*/

	}

	String makePayStr(){
		return "alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2017081108144602&biz_content=%7B%22body%22%3A%22%E9%80%9F%E6%8B%89%E9%80%9F%E8%BF%90%22%2C%22out_trade_no%22%3A%22sf5412454669845d522a1%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22App%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95Java%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=utf-8&format=json&method=alipay.trade.wap.pay&notify_url=http%3A%2F%2Fdomain.com%2FCallBack%2Fnotify_url.jsp&return_url=http%3A%2F%2Fdomain.com%2FCallBack%2Freturn_url.jsp&sign=VlQ2oK9dfAprri2feHuDyzNcCXhvgOXyO2GSzrMbIKzPinMIENcD0oqvp7SwexIq4TL30jZlFNhpplerX6%2BLzXge3%2FuqATuIiqPAvk6%2F3aEm9ysMHIWG79p9D7I%2B53SV4F9wOjoOTcbb9LT1iof%2Bv7A5nLE1Yv0s7TWCCH5A5Yf0VL06hMOZligzUqvgkcIIrkSjjRkTlrcpmSz0JvBKnEDUqV3IJmaTIZoId6P6ywua8X%2FFfkbQasExaJqFcqDTAHNPEz9FD6U5ekXMK2oqtBo%2BtJQOJKgMGf4duz8DJboamtjO0RXbaMAijbXMdgzPGA50gK4wwfymgYLFdtJuqA%3D%3D&sign_type=RSA2&timestamp=2017-08-14+22%3A26%3A03&version=1.0";
	}

	/**
	 * 支付宝支付
	 * @param act
	 * @param listener
	 */
	public void payAlipay( String orderContent,
						   final Activity act, final OnPayCompleteResyltListener listener){
		UtilMethod.showProgressDialog(act, "跳转支付宝中...");
		if(StringUtils.isEmpty(orderContent)){
			orderContent = makePayStr();
		}

//		AlipayHelper.checkAlipay(act, new AlipayHelper.onAlipayChecklistener() {
//			@Override
//			public void checkAccount(boolean isExist) {
//				if(isExist){
					AlipayHelper.payByAlipay(orderContent, act, new AlipayHelper.onAlipayPaylistener() {
						@Override
						public void payResult(String result) {
							UtilMethod.dismissProgressDialog(act);
							if(result==null){
								showOrderDialog(result,"支付出错,请到“个人中心”查看订单", false, act, listener);
							}else{
								try {
									PayResult payResult = new PayResult(result);
									String resultStatus = payResult.getResult();
									// 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
									if (TextUtils.equals(resultStatus, "9000")) {
										showOrderDialog(result,"支付成功", true, act, listener);
									}else{
										// 判断resultStatus 为非“9000”则代表可能支付失败
										// “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
										if (TextUtils.equals(resultStatus, "8000")) {
											showOrderDialog(result,"支付结果确认中", false, act, listener);
										}else if(TextUtils.equals(resultStatus, "6001")){
											//用户取消交易
											showOrderDialog(result,"支付取消", false, act, listener);
//											ToastUtil.showMessage(act, "支付取消");
										} else {
											// 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
											showOrderDialog(result,"支付失败,请到“个人中心”查看订单", false, act, listener);
										}
									}
								} catch (Exception e) {
									e.printStackTrace();
									showOrderDialog(result,"支付失败,请到“个人中心”查看订单", false, act, listener);
								}
							}
						}
					});
//				}else{
//					UtilMethod.dismissProgressDialog(act);
//					showOrderDialog("","支付宝帐号不可用", false, act, listener);
//				}
//			}
//		});
	}




	private void showOrderDialog(String resultStr,String txt,final boolean result, Activity act, final OnPayCompleteResyltListener listener){
		listener.OnResult(resultStr,txt,result);

//		ConfirmSingleBtnDialog dialog = new ConfirmSingleBtnDialog(act, new ConfirmSingleBtnDialog.PriorityListener() {
//			@Override
//			public void refreshPriorityUI() {
//				listener.OnResult(result);
//			}
//		});
//		dialog.setPromptContext(txt);
//		dialog.setCloseable(false);
//		dialog.setCancelable(false);
//		dialog.show();
	}
	
}
